/*
 * @Author: sanshichen
 * @Date: 2018-04-06 07:57:28
 * @Last Modified by: sanshichen
 * @Last Modified time: 2018-04-06 11:26:08
 * @Description: 解析markdown文档
*/
'use strict';
const path = require('path');
const fs = require('fs');
const fm = require('front-matter');
const MarkdownIt = require('markdown-it');
const { readFile } = require('../extend/helper');

module.exports = source => {
  return async () => {
    const md = new MarkdownIt({ html: true, linkify: true });
    const isExistFile = fs.existsSync(source);
    if (!isExistFile) {
      return false;
    }
    return await readFile(source).then(data => {
      const frontmatter = fm(data);
      frontmatter.attributes.key = path.basename(source, '.md');
      frontmatter.attributes.html = md.render(frontmatter.body);
      return frontmatter.attributes;
    });
  };
};
